<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02.处理服务器端返回的JSON数据</title>
  </head>

  <body>

  </body>
  <script>
    //1.创建ajax对象
    const xhr = new XMLHttpRequest();
    // 2.告诉ajax往哪里发送请求，以什么方式发送请求
    // 参数:[请求方式，请求地址]
    xhr.open('get', "http://127.0.0.1:3000/ajaxGetData");
    // 3.发送ajax
    xhr.send();
    // 4.获取服务器端响应到客户端的数据
    // xhr.onreadystatechange = function () {
    xhr.onload = function () {
      console.log(xhr.response); //{"name":"jason","age":18}
      //返回的是一个JSON字符串
      console.log(typeof (xhr.response)); //String
      //将JSON字符串转换为JSON对象
      let xhrStr = JSON.parse(xhr.response);
      console.log(xhrStr); //{name: "jason", age: 18}
      console.log(typeof (xhrStr)); //Object
    }
  </script>

</html>